namespace C1.Win.C1FlexGrid
{
    using C1.Win.C1FlexGrid.Util;
    using System;
    using System.ComponentModel;
    using System.Reflection;

    public class Column : RowCol
    {
        internal string _name;
        internal SortFlags _sort;

        internal Column(ColumnCollection coll, int index)
        {
            base._coll = coll;
            base._dataIndex = index;
        }

        internal string BuildString(bool includeDefault)
        {
            string str = string.Empty;
            foreach (PropertyInfo info in base.GetType().GetProperties())
            {
                if (this.IsBrowsable(info))
                {
                    object obj2 = info.GetValue(this, null);
                    if ((obj2 != null) && (includeDefault || !this.IsDefaultValue(info, obj2)))
                    {
                        if (obj2 is string)
                        {
                            string str2 = (string) obj2;
                            if (str2.Length == 0)
                            {
                                goto Label_00EE;
                            }
                            str2 = str2.Replace('\t', ' ');
                            if (((str2.Length > 1) && str2.StartsWith("\"")) && str2.EndsWith("\""))
                            {
                                str2 = str2.Substring(1, str2.Length - 2);
                            }
                            obj2 = "\"" + str2.Replace("\"", "\"\"") + "\"";
                        }
                        str = str + string.Format("{0}:{1};", info.Name, Types.ToString(obj2));
                    Label_00EE:;
                    }
                }
            }
            return str;
        }

        public override void Clear(ClearFlags clear)
        {
            base.Clear(clear);
            if (((clear & ClearFlags.Content) != 0) && (base._coll != null))
            {
                GridModel model = base._coll._grid;
                int col = this.Index;
                for (int i = 0; i < model._rows.Count; i++)
                {
                    model.ClearCell(i, col, ClearFlags.Content);
                }
            }
        }

        internal string GetColumnProperty(string str, string name)
        {
            int start = this.IndexOfUnquoted(str, name + ":");
            if (start < 0)
            {
                return null;
            }
            start += name.Length;
            int num2 = this.IndexOfUnquoted(str, ";", start);
            if (num2 < 0)
            {
                return null;
            }
            str = str.Substring(start + 1, (num2 - start) - 1);
            if (((str.Length > 1) && str.StartsWith("\"")) && str.EndsWith("\""))
            {
                str = str.Substring(1, str.Length - 2);
                str = str.Replace("\"\"", "\"");
            }
            return str;
        }

        internal int IndexOfUnquoted(string str, string strFind)
        {
            return this.IndexOfUnquoted(str, strFind, 0);
        }

        internal int IndexOfUnquoted(string str, string strFind, int start)
        {
            int index = str.IndexOf(strFind, start);
            while (index > -1)
            {
                bool flag = false;
                for (int i = 0; i < index; i++)
                {
                    if (str[i] == "\""[0])
                    {
                        flag = !flag;
                    }
                }
                if (!flag)
                {
                    return index;
                }
                index = str.IndexOf(strFind, (int) (index + 1));
            }
            return index;
        }

        internal bool IsBrowsable(PropertyInfo pi)
        {
            return (pi.CanRead && pi.CanWrite);
        }

        internal bool IsDefaultValue(PropertyInfo pi, object value)
        {
            if (value == null)
            {
                return true;
            }
            object[] customAttributes = pi.GetCustomAttributes(typeof(DefaultValueAttribute), true);
            if (customAttributes.Length == 0)
            {
                return false;
            }
            DefaultValueAttribute attribute = (DefaultValueAttribute) customAttributes[0];
            return value.Equals(attribute.Value);
        }

        internal bool ParseString(string str)
        {
            foreach (PropertyInfo info in base.GetType().GetProperties())
            {
                if (this.IsBrowsable(info))
                {
                    string columnProperty = this.GetColumnProperty(str, info.Name);
                    if (columnProperty != null)
                    {
                        try
                        {
                            object obj2 = Types.FromString(columnProperty, info.PropertyType);
                            info.SetValue(this, obj2, null);
                        }
                        catch
                        {
                        }
                    }
                }
            }
            return true;
        }

        [DefaultValue(true)]
        public bool AllowSorting
        {
            get
            {
                return (((short) (base._flags & RowColFlags.NoSort)) == 0);
            }
            set
            {
                if (this.AllowSorting != value)
                {
                    base.SetFlags(RowColFlags.NoSort, !value);
                }
            }
        }

        [DefaultValue((string) null)]
        public string Caption
        {
            get
            {
                if (base._coll != null)
                {
                    GridModel model = base._coll._grid;
                    if ((model != null) && (model._rows._fixed > 0))
                    {
                        return model.GetDataDisplay(0, this.SafeIndex);
                    }
                }
                return string.Empty;
            }
            set
            {
                if (base._coll != null)
                {
                    GridModel model = base._coll._grid;
                    if ((model != null) && (model._rows._fixed > 0))
                    {
                        model.SetData(0, this.SafeIndex, value, false);
                    }
                }
            }
        }

        public int DataIndex
        {
            get
            {
                if (base._coll != null)
                {
                    GridModel model = base._coll._grid;
                    if (model.IsDataBound())
                    {
                        return model._dataSource.ColumnIndex(this._name);
                    }
                    int num = this.Index - base._coll._fixed;
                    if (num >= 0)
                    {
                        return num;
                    }
                }
                return -1;
            }
        }

        public override int Index
        {
            get
            {
                if (base._coll == null)
                {
                    return -1;
                }
                return base._coll._grid._cols._items.IndexOf(this);
            }
        }

        public int Left
        {
            get
            {
                return base.Beg;
            }
        }

        [DefaultValue((string) null)]
        public string Name
        {
            get
            {
                if (this._name == null)
                {
                    return string.Empty;
                }
                return this._name;
            }
            set
            {
                this._name = value;
                if (base._coll != null)
                {
                    base._coll.NotifyViews(GridChangedTypeEnum.RepaintGrid);
                }
            }
        }

        public int Right
        {
            get
            {
                return base.End;
            }
        }

        public virtual int SafeIndex
        {
            get
            {
                int num = this.Index;
                if (num < 0)
                {
                    throw new Exception("Column does not belong to grid.");
                }
                return num;
            }
        }

        public override bool Selected
        {
            get
            {
                return (((short) (base._flags & RowColFlags.Selected)) != 0);
            }
            set
            {
                if (this.Selected != value)
                {
                    base.SetFlags(RowColFlags.Selected, value);
                    if (base._coll != null)
                    {
                        base._coll.NotifyViews(GridChangedTypeEnum.ColSelected, -1, this.Index);
                    }
                }
            }
        }

        public SortFlags Sort
        {
            get
            {
                return this._sort;
            }
            set
            {
                this._sort = value;
            }
        }

        [DefaultValue(-1)]
        public int Width
        {
            get
            {
                return base.Size;
            }
            set
            {
                base.Size = value;
            }
        }

        public int WidthDisplay
        {
            get
            {
                return base.SizeDisplay;
            }
            set
            {
                base.SizeDisplay = value;
            }
        }
    }
}

